package com.yanqu.road.dao.impl.activity.oldplayerreturn;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.oldplayerreturn.OldPlayerReturnAddtionGood;
import com.yanqu.road.entity.activity.oldplayerreturn.OldPlayerReturnTaskRewardData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class OldPlayerReturnTaskRewardDataDaoImpl extends TempDataObjectDao<OldPlayerReturnTaskRewardData> {
    public static String getOldPlayerReturnTaskRewardDataSql = "select * from `t_u_return_player_task_reward` where `activityId`=? and `userId`=?";
    public Map<Integer,OldPlayerReturnTaskRewardData> getOldPlayerReturnTaskRewardData(int activityId,long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer,OldPlayerReturnTaskRewardData> oldPlayerReturnTaskRewardDataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getOldPlayerReturnTaskRewardDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                OldPlayerReturnTaskRewardData taskRewardData = parse(rs);
                oldPlayerReturnTaskRewardDataMap.put(taskRewardData.getConditionId(),taskRewardData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return oldPlayerReturnTaskRewardDataMap;
    }

    public static String updateOldPlayerReturnTaskRewardDataSql = "update `t_u_return_player_task_reward` set conditionReward=? where activityId=? and userId=? and conditionId=?";
    @Override
    protected boolean updateData(OldPlayerReturnTaskRewardData oldPlayerReturnTaskRewardData) {
        oldPlayerReturnTaskRewardData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(oldPlayerReturnTaskRewardData.getConditionReward())));
        para.put(i++, new DbParameter(Types.INTEGER, oldPlayerReturnTaskRewardData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, oldPlayerReturnTaskRewardData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, oldPlayerReturnTaskRewardData.getConditionId()));
        if (getDb().executeNoneQuery(updateOldPlayerReturnTaskRewardDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertOldPlayerReturnTaskRewardDataSql = "insert into `t_u_return_player_task_reward`(activityId,userId,conditionId,conditionReward) values (?,?,?,?)";
    @Override
    protected boolean addData(OldPlayerReturnTaskRewardData oldPlayerReturnTaskRewardData) {
        oldPlayerReturnTaskRewardData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, oldPlayerReturnTaskRewardData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, oldPlayerReturnTaskRewardData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, oldPlayerReturnTaskRewardData.getConditionId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(oldPlayerReturnTaskRewardData.getConditionReward())));
        if (getDb().executeNoneQuery(insertOldPlayerReturnTaskRewardDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected OldPlayerReturnTaskRewardData parseData(ResultSet rs) throws SQLException {
        OldPlayerReturnTaskRewardData oldPlayerReturnTaskRewardData = new OldPlayerReturnTaskRewardData();
        oldPlayerReturnTaskRewardData.setActivityId(rs.getInt("activityId"));
        oldPlayerReturnTaskRewardData.setUserId(rs.getLong("userId"));
        oldPlayerReturnTaskRewardData.setConditionId(rs.getInt("conditionId"));
        oldPlayerReturnTaskRewardData.setConditionReward(JSON.parseObject(rs.getString("conditionReward"), new TypeReference<List<OldPlayerReturnAddtionGood>>() {}));

        return oldPlayerReturnTaskRewardData;
    }

}
